﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace AMix
{
    public partial class ComponenteFrm : FrmRezumat
    {
        private Int32 idMF = -1;
        public ComponenteFrm(Int32 _idMF, ModuleClass mInfo)
            : base(mInfo)
        {
            idMF = _idMF;
            InitializeComponent();
            eSRPDataSet.AmixComponenteSecundare.CantitateColumn.DefaultValue = 1;
            eSRPDataSet.AmixComponenteSecundare.PretColumn.DefaultValue = 0;
            eSRPDataSet.AmixComponenteSecundare.ValoareColumn.DefaultValue = 0;
            this.HandleDel += new EventHandler(Sterge);

        }

        private void ComponenteFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            departamenteTableAdapter.Connection = con;
            departamenteTableAdapter.Fill(eSRPDataSet.Departamente, ModuleInfo.CodUnitate);
            this.amixRegistruTableAdapter.Connection = con;
            this.amixRegistruTableAdapter.Fill(this.eSRPDataSet.AmixRegistru, ModuleInfo.CodUnitate,0,5);
            this.amixComponenteSecundareTableAdapter.Connection = con;
            if (idMF == -1)
                this.amixComponenteSecundareTableAdapter.Fill(this.eSRPDataSet.AmixComponenteSecundare);
            else
                amixComponenteSecundareTableAdapter.FillBy(eSRPDataSet.AmixComponenteSecundare, idMF);

            InchideConexiune(con);
            AddButton("Salveaza", global::ERPStudio.Properties.Resources.salvare, Salvare, ToolStripItemAlignment.Right, true);
           
            this.listBndSourceMain.Add(fKAmixComponenteSecundareAmixRegistruBindingSource);
            listTableAdaptor.Add(amixComponenteSecundareTableAdapter.Adapter);
            
        }

        private void componenteDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            if (e.ColumnIndex==cantitateColumn.Index || e.ColumnIndex == pretColumn.Index)
            {
                Decimal newVal = Convert.ToDecimal(componenteDGV[cantitateColumn.Index,e.RowIndex].Value) *
                    Convert.ToDecimal(componenteDGV[pretColumn.Index, e.RowIndex].Value);
                if (Convert.ToDecimal(componenteDGV[valoareColumn.Index, e.RowIndex].Value) != newVal)
                    componenteDGV[valoareColumn.Index, e.RowIndex].Value = newVal;
                sumaLL.Text = GetSum().ToString();
            }
        }

        private Decimal GetSum()
        {
            Decimal suma = Decimal.Zero;
            foreach (DataGridViewRow dr in componenteDGV.Rows)
            {
                suma += Convert.ToDecimal(dr.Cells[valoareColumn.Index].Value);
            }
            return suma;
        }

        private void ComponenteFrm_Activated(object sender, EventArgs e)
        {
            sumaLL.Text = GetSum().ToString();
        }
        new public void Sterge(object sender, EventArgs args)
        {
            sumaLL.Text = GetSum().ToString();
        }
    }
}
